package com.sgg.datastructure.stack;

/**
 * 数组实现
 * 0.存储数据
 * 1.构造器
 * 2.满
 * 3.空
 * 4.push
 * 5.pop
 * 6.遍历
 */
public class ArrayStack {
    private int[] array;
    private int size;
    private int top=-1;

    public ArrayStack(int size) {
        this.size = size;
        array = new int[size];
    }

    public int getSize() {
        return size;
    }

    public boolean isFull(){
        return top == size - 1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public void push(int val) {
        if (isFull()) {
            throw new RuntimeException("栈已满,无法push");
        }
        top++;
        array[top] = val;
    }

    public int pop(){
        if (isEmpty()) {
            throw new RuntimeException("栈是空的,无法pop");
        }
        int result = array[top];
        top--;
        return result;
    }

    public void show(){
        for (int i = top; i >=0 ; i--) {
            System.out.printf("stack[%d]=%d\n",i,array[i]);
        }
    }

    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(5);
        arrayStack.show();
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
    }
}
